package com.gaojun.service;

import com.spring.BeanPostProcessor;
import com.spring.Component;
import com.spring.GJValue;

import java.lang.reflect.Field;

/**
 * @author gaojun
 * @description TODO
 * @date 2022/7/3 20:48
 */
@Component
public class GJValueBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        for (Field field : bean.getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(GJValue.class)) {
                GJValue gjValueAnnotation = field.getAnnotation(GJValue.class);
                String value = gjValueAnnotation.value();
                field.setAccessible(true);
                try {
                    field.set(bean,value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return bean;
    }
}
